using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SunshineFarmWord
{
    public class PlayerHurtState : PlayerBaseStates
    {
        public PlayerHurtState(PlayerStateMachine playerStateMachine) : base(playerStateMachine)
        {
            if (stateMachine.ReusableData.PlayerStatesData.PlayerHurtData == null) stateMachine.ReusableData.PlayerStatesData.PlayerHurtData = new PlayerHurtData();
        }

        #region Base function
        public override void Enter()
        {
            base.Enter();

            StartAnimation(stateMachine.Player.AnimationData.HurtParameterHash);

            InitReusableData();

            //后退一段距离
            BackDistance();
            //扣血
            DeductLife();
        }

        public override void Exit()
        {
            base.Exit();

            ClearReusableData();
            //停止向后退
            stateMachine.Player.TakeDistanceOver();

            StopAnimation(stateMachine.Player.AnimationData.HurtParameterHash);
        }
        #endregion


        #region 动画监听
        public override void OnAnimationExitEvent()
        {
            base.OnAnimationExitEvent();
            //退出受击状态
            stateMachine.ChangeState(stateMachine.IdlingState);
        }
        #endregion


        #region Main function
        //扣血
        private void DeductLife()
        {
        }
        //初始化数据
        private void InitReusableData()
        {
            stateMachine.ReusableData.PlayerStatesData.PlayerHurtData.BackDistance = playerStatesData.PlayerHurtData.BackDistance;
        }
        // 清空数据
        private void ClearReusableData()
        {
            stateMachine.ReusableData.PlayerStatesData.PlayerHurtData.HurtSourceNormal = Vector2.zero;
        }

        //后退一段距离
        private void BackDistance()
        {
            stateMachine.Player.TakeDistance(stateMachine.ReusableData.PlayerStatesData.PlayerHurtData.HurtSourceNormal * stateMachine.ReusableData.PlayerStatesData.PlayerHurtData.BackDistance, 2.2f * Time.deltaTime);
        }
        #endregion
    }
}
